package com.gu.qqdemo.adapter;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.gu.qqdemo.Bean.FeatureBean;
import com.gu.qqdemo.R;
import com.gu.qqdemo.dao.FeatureDao;

import java.util.List;

public class MoreNewsAdapter extends RecyclerView.Adapter<MoreNewsAdapter.BaseViewHolder> {
    //存放已关闭的功能的集合
    private List<FeatureBean> listclose;
    //存放已开启的功能的集合
    private List<FeatureBean> listopen;
    private FeatureDao daos;

    public MoreNewsAdapter(List<FeatureBean> listclose, List<FeatureBean> listopen) {
        this.listclose = listclose;
        this.listopen = listopen;
        daos = new FeatureDao();
    }

    private final int type_head = 1;
    private final int type_bean = 2;

    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return type_head;
        } else if (position < 1 + listclose.size()) {
            return type_bean;
        } else if (position == 1 + listclose.size()) {
            return type_head;
        } else {
            return type_bean;
        }
    }

    @NonNull
    @Override
    public BaseViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        BaseViewHolder vh = null;
        switch (viewType) {
            case type_head:
                View vhead = LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.act_more_adapter_head, parent, false);
                vh = new headViewHolder(vhead);
                break;
            case type_bean:
                View view = LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.act_more_adapter, parent, false);
                vh = new beanViewHolder(view);
                break;
        }
        return vh;
    }

    @Override
    public void onBindViewHolder(@NonNull BaseViewHolder holder, int position) {
        if (holder instanceof beanViewHolder) {
            beanViewHolder vh = (beanViewHolder) holder;
            if (position < 1 + listclose.size()) {
                FeatureBean bean = listclose.get(position - 1);
                vh.setDate(bean);
            } else {
                FeatureBean bean = listopen.get(position - listclose.size() - 2);
                vh.setDate(bean);
            }
        } else if (holder instanceof headViewHolder) {
            headViewHolder vh = (headViewHolder) holder;
            vh.setDate(position);
        }


    }

    @Override
    public int getItemCount() {
        return listclose.size() + listopen.size() + 2;
    }

    public class BaseViewHolder extends RecyclerView.ViewHolder {


        public BaseViewHolder(View itemView) {
            super(itemView);
        }

//        public void setDate(FeatureBean bean) {
//
//        }
    }

    public class headViewHolder extends BaseViewHolder {
        private TextView more_head;

        public headViewHolder(View itemView) {
            super(itemView);
            more_head = itemView.findViewById(R.id.more_head);
        }

        public void setDate(int position) {
            more_head.setText(position == 0 ? "已关闭的功能" : "已开启的功能");
        }
    }

    public class beanViewHolder extends BaseViewHolder implements View.OnClickListener {
        private ImageView more_iv;
        private TextView more_tv;
        private Button more_bt;
        private FeatureBean bean;

        public beanViewHolder(View itemView) {
            super(itemView);
            more_iv = itemView.findViewById(R.id.more_iv);
            more_tv = itemView.findViewById(R.id.more_tv);
            more_bt = itemView.findViewById(R.id.more_bt);

            more_bt.setOnClickListener(this);
        }

        public void setDate(FeatureBean bean) {
            this.bean = bean;

            more_tv.setText(bean.getName());
            //如果showtype的值为0 就设为开启按钮 否则反之
            if ("0".equals(bean.getShowType())) {
                more_bt.setText("开启");
            } else {
                more_bt.setText("关闭");
            }
        }

        @Override
        public void onClick(View v) {
            if (v == more_bt) {
                //修改数据库
                bean.setShowType("0".equals(bean.getShowType()) ? "1" : "0");
                daos.update(bean);
                //清除list中的数据
                listclose.clear();
                listopen.clear();
                //重新添加数据
                listclose.addAll(daos.findAllClose());
                listopen.addAll(daos.findAllOpen());
                //刷新界面
                notifyDataSetChanged();
            }
        }
    }
}
